package com.itheima.springbootwebtlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.springbootwebtlias.anno.LogOperation;
import com.itheima.springbootwebtlias.exception.PasswordErrorException;
import com.itheima.springbootwebtlias.exception.UsernameNotExistException;
import com.itheima.springbootwebtlias.mapper.EmpExprMapper;
import com.itheima.springbootwebtlias.mapper.EmpMapper;
import com.itheima.springbootwebtlias.pojo.Emp;
import com.itheima.springbootwebtlias.pojo.EmpExpr;
import com.itheima.springbootwebtlias.pojo.EmpQueryParam;
import com.itheima.springbootwebtlias.pojo.PageResult;
import com.itheima.springbootwebtlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    //分页+条件 查询
    @Transactional(rollbackFor = Exception.class)
    @Override
    public PageResult page(EmpQueryParam empQueryParam) {
        //1.启动分页插件
        log.info("分页查询，参数：{}", empQueryParam);
        //1. 设置分页参数
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());

        //2. 执行查询
        Page<Emp> page = (Page<Emp>) empMapper.selectByCondition(empQueryParam);

        //3.调用分页插件的增强持久层方法
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());

        return pageResult;
    }

    //添加员工
    @LogOperation
    @Override
    public void insert(Emp emp) {
        //1.添加员工信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);

        //2.添加员工经历
        List<EmpExpr> empExprList = emp.getExprList();
        if (empExprList != null && empExprList.size() > 0) {
            empExprList.stream().forEach(ex -> ex.setEmpId(emp.getId()));
            //批量插入
            empExprMapper.insertBatch(empExprList);

            log.info("批量插入员工经历，参数：{}", empExprList);
        }
    }

    //查询所有员工
    @Override
    public List<Emp> queryAllEmp() {
        List<Emp> empList = empMapper.queryAllEmp();
        return empList;
    }

    //批量删除员工
    //加入事务注解
    @LogOperation
    //@Transactional
    @Override
    public void removeBatch(List<Integer> ids) {
        //1.调用持久层EmpMapper，批量删除员工
        empMapper.deleteBatchById(ids);
        //2.调用持久层EmpExprMapper，批量删除员工经历
        empExprMapper.deleteBatchByEmpId(ids);
    }

    //查询员工详情+员工经历 ，回显
    @Override
    public Emp queryById(Integer id) {
        //方案一：两次单表查询
        //1.调用持久层EmpMapper，查询员工详情
        Emp emp = empMapper.selectById(id);   //单表查询

        //2.调用持久层EmpExprMapper，查询员工经历
        List<EmpExpr> exprList = empExprMapper.selectByEmpId(id); //单表查询

        //3.封装Emp返回
        emp.setExprList(exprList);

        return emp;
    }

    // 修改更新员工信息和工作经历
    @LogOperation
    @Override
    public void update(Emp emp) {
        //1.更新员工信息，同步更新时间
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);

        //2.调用持久层EmpMapper，更新员工工作经历信息
        //2.1.删除员工工作经历
        List<Integer> ids = new ArrayList<>();
        ids.add(emp.getId());
        empExprMapper.deleteBatchByEmpId(ids);

        //2.2.批量插入员工工作经历
        if (emp.getExprList() != null && emp.getExprList().size() > 0) {
            //设置新增的所属员工信息
            emp.getExprList().stream()
                    .forEach(ex -> ex.setEmpId(emp.getId()));
            empExprMapper.insertBatch(emp.getExprList());
        }
    }

    //登录
    @Override
    public Emp checkLogin(Emp emp) {
        //1.调用持久层EmpMapper，根据用户名查询员工信息
        Emp resultEmp = empMapper.selectByUsername(emp.getUsername());
        //2.判断员工账号是否存在
        if (resultEmp == null) {
            //业务异常，走全局异常处理机制
            throw new UsernameNotExistException("用户名不存在");
        }
        //3.判断密码是否正确
        if (!Objects.equals(emp.getPassword(), resultEmp.getPassword())) {
            //业务异常，走全局异常处理机制
            throw new PasswordErrorException("密码错误");
        }
        //4.登录成功，返回员工信息
        return resultEmp;
    }
}
